if (!ubi) ubi= {}
ubi.block = function (b, view) {
this.id = b.id;
this.content = b.content;
this.tags= [];
this.is_fold = b.fold ? 1: 0;
this.is_select = false;
this.create_time = b.create_time;
this.modify_time = b.modify_time;
this.div = '#' + this.id + '_block-item';
this.json = b;
this.father = view;

this.init = function () {
    if (this.is_fold)
        this.fold();
    else 
        this.unfold();
    // add me to nicedit as a new instance
    global.niced.addInstance(this.id + '_block-item-content')
    $(this.div).find('.small-btn-base').hide();
};

/* return block html
 * */
this.html = function () {
    return form_block(this.json);
}

/* collapse block.
 * */
this.fold = function (save) {
    $(this.div).find('.block-item-content, .block-item-properties').hide();
    $(this.div).find('.block-item-brief').show();
    $(this.div).find('.block-item-fold-btn').css('background-image', 'url(../imgs/icons/16x16/toggle-fold.png)');

    this.is_fold = 1;
}

/* unfold me.
 * */
this.unfold = function () {
    $(this.div).find('.block-item-content, .block-item-properties').show();
    $(this.div).find('.block-item-brief').hide();
    $(this.div).find('.block-item-fold-btn').css('background-image', 'url(../imgs/icons/16x16/toggle-unfold.png)');
    this.is_fold = 0;

}

/* toggle state of fold/unfold
 * */
this.toggle_fold = function () {
    if (this.is_fold) {
        this.fold();
    } else {
        this.unfold();
    }
}

/* set me as selected
 * */
this.select = function () {
    $(this.div).addClass('block-item-selected').addClass('with-light');
    this.is_select = true;
}

/* disselect me
 * */
this.disselect = function () {
    $(this.div).removeClass('block-item-selected').removeClass('with-light');
    this.is_select = false;
}

/* toggle selected/disselected
 * */
this.toggle_select = function () {
    if (this.is_select)
        this.disselect();
    else
        this.select();
}
/* delete me.
 * */
this.remove = function () {
    var _this = this;
    remove_block_do(this.id, 
    function (state, data) {
        var id = data['id'];
        $(_this.div).remove();
        notify('Remove note `'
            + data['content'] 
            + '` done. You can not Undo yet.');
        delete _this.father.blocks[_this.id];
    },
    function (state, data) {
        notify('Remove block(s) fail.');    
    });
}

/* save fold/unfold state to database
 * */
this.save_state = function () {
    save_block_state_do(this.id, this.is_fold);
}


this.bind_events = function() {
    this.bind_mousedown();
    this.bind_contents_click();
    this.bind_remove_btn_click();
    this.bind_cite_btn_click();
    this.bind_tag_btn_click();
    this.bind_edit_btn_click();
    this.bind_pin_btn_click();
    this.bind_moveto_btn_click();

    this.bind_fold_btn_click();

    this.bind_hover();
    this.bind_click();
    this.bind_cite_link_click();
};


this.update_tags = function () {
    var _this = this;
    update_block_tags_do (this.id,
        function (state, data) {
            var id = data['block_id'];
            var tags = data['tags'];
            $(_this.div).find('.block-item-tag').remove();
            var tag_bar = $(_this.div).find('.block-item-tag-bar');
            for (var i = 0; i < tags.length; i+=1) {
                var t = $('<a class="block-item-tag with-radius-border-10" href="tag:'+ tags[i].name +'" title="tagged by '
                    + tags[i].count + ' notes."'
                    +'>'+ tags[i].name +'</a>');
                t.click(function () {
                        search_view.open(t.attr('href'));
                    });
                tag_bar.append(t);
            }
        },
        function (state, data) {
            //pass  
        });
} 

/* deal with cite links clicked event in block content
 * */
this.bind_cite_link_click = function () {
    $(this.div).find('.cite-link').click (function (event) {
        trigger_citelink_click(this)
        return false;
    });
}

/* deal with the clicked event, the select action, at the present time. 
 * */
this.bind_click = function () {
    var _this = this;
    $(this.div).click (function (event) {
        if (event.ctrlKey == 1)  //select mode
            _this.toggle_select();
    });
}

/* deal with the mousedown event. do nothing but stop the event bubbles.
 * */
this.bind_mousedown = function () {
    $(this.div).find('.block-item-inner').unbind('mousedown');        
    $(this.div).find('.block-item-inner')
    .bind('mousedown', function(event){ 
        //prevent the bubble, but not default events
        event.stopPropagation();
    });    
}


this.bind_contents_click = function() {
    var _this = this;
    $(this.div).find('.block-item-content').unbind('click');
    $(this.div).find('.block-item-content').click(function (event){
        if (event.ctrlKey == 1)
            return;
        var id = _this.id
        global.crr_block_id = id;
        _this.father.modified_block_ids[id] = 1;
        //switch_toolbar('#note-edit-bar');
        
        event.stopPropagation();
    });
}


this.bind_remove_btn_click= function() {
    var _this = this;
    $(this.div).find('.block-item-remove-btn').unbind('click');    
    $(this.div).find('.block-item-remove-btn').click(function (){
        if(confirm('Are you sure you want to permanently delete this note?'))
        {
            _this.remove()
        }
    });
}

this.bind_cite_btn_click= function() {
    var _this = this;
    $(this.div).find('.block-item-cite-btn').unbind('click');    
    $(this.div).find('.block-item-cite-btn').click(function (){
        var note_id = _this.father.id;
        var block_id = _this.id;
        global.clipboard.cite_link('cite://'+note_id+'-'+block_id);
        notify('Citation was copyed.')
    });
}

this.bind_edit_btn_click= function() {
    var _this = this;
    $(this.div).find('.block-item-edit-btn').unbind('click');    
    $(this.div).find('.block-item-edit-btn').click(function (){
        var note_id = _this.father.id;
        var block_id = _this.id;
        alert('you click edit btn');
    });
}
this.bind_pin_btn_click= function() {
    var _this = this;
    $(this.div).find('.block-item-pin-btn').unbind('click');    
    $(this.div).find('.block-item-pin-btn').click(function (){
        var note_id = _this.father.id;
        var block_id = _this.id;
        alert('you click pin btn');
    });
}
this.bind_moveto_btn_click= function() {
    var _this = this;
    $(this.div).find('.block-item-moveto-btn').unbind('click');    
    $(this.div).find('.block-item-moveto-btn').click(function (){
        var note_id = _this.father.id;
        var block_id = _this.id;
        alert('you click moveto btn');
    });
}



this.bind_fold_btn_click = function() {
    var _this = this;
    $(this.div).find('.block-item-fold-btn').unbind('click');    
    $(this.div).find('.block-item-fold-btn').click(function (){
        if (_this.is_fold) {
            _this.unfold()
        } else {
            _this.fold();
        }
        _this.save_state();
    });
}

this.bind_hover = function() {
    var _this = this;
    $(this.div).find('.block-item-inner').hover(function () {
            $(_this.div).find('.small-btn-base').show();
        },
        function () {
            $(_this.div).find('.small-btn-base').hide();
        }
    );
}

this.bind_tag_btn_click = function() {
    var _this = this;
    $(this.div).find('.block-item-tag-btn').unbind('click')
    $(this.div).find('.block-item-tag-btn').click(function () {
            var id = _this.id;
            var tag_bar = $(_this.div).find('.block-item-tag-bar');
            var tags = []
            tag_bar.find('.block-item-tag').each(function () {
                tags.push($(this).text());
            })
            var ret = prompt('Edit note tags', tags.join(','));
            if (ret == null) {
                return;
            }
            tags = []
            tags.push('[')
            $.each(ret.split(','),function(i,n) {
                tags.push('"');
                tags.push(n);
                tags.push('"');
                tags.push(',')
            });
            tags.pop(); //last ","
            tags.push(']');
            save_block_tag_do(id, tags.join(''),
                function (state, data) {
                    _this.update_tags();
                },
                function (state, data) {
                    notify('Save block tags fail.');    
                }
            );
            
        }
    );
}


//this.init();
}
